import { regionDetail, relationArticleList, relationLiteratureList } from "../../../api/index";
import { wxNav } from "../../../utils/util";

// pages/tabBar/area/index.ts
Page({

    /**
     * 页面的初始数据
     */
    data: {
        dictData: [
            {
                id: ""
            }
        ],
        id: "",
        active: 0,
        areaData: { id: "" },
        literatureData: [],
        articleData: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
        const app = getApp();
        // 获取实例上存储的数据
        this.setData({
            dictData: app.globalData.areaDict,
            id: app.globalData.areaDict[this.data.active].id
        });
    },

    getData(id: string) {
        regionDetail(id).then((r: any) => this.setData({ areaData: r }));
        relationLiteratureList(id).then((r: any) => this.setData({ literatureData: r }));
        relationArticleList(id).then((r: any) => this.setData({ articleData: r }))
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getData(this.data.id);
    },

    tabChange(e: { detail: { index: number } }) {
        const i = e.detail.index;
        this.getData(this.data.dictData[i].id);
        this.setData({
            id: this.data.dictData[i].id,
            active: i
        })
    },

    // 详情
    detailClick() {
        wxNav("/pages/area/detail/index?id=" + this.data.id)
    }
})